package nl.hanze.designpatterns.mvc.application;

import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JFrame;

import nl.hanze.designpatterns.domain.Answer;
import nl.hanze.designpatterns.domain.LoginCredential;
import nl.hanze.designpatterns.domain.Question;
import nl.hanze.designpatterns.mvc.gui.DashboardGUI;
import nl.hanze.designpatterns.mvc.gui.GUI;
import nl.hanze.designpatterns.mvc.gui.LoginGUI;

public class Application {

	private static LoginGUI loginGUI;
	private static DashboardGUI dashboardGUI;
	private static LoginCredential currentUser;
	
	public static void startLoginGUI(){
		if(loginGUI == null){
			loginGUI = new LoginGUI();
			loginGUI.setVisible(true);
		}
	}
	
	public static void closeLoginGUI(){
		if(loginGUI != null){
			loginGUI.setVisible(false);
			loginGUI.dispose();
			loginGUI = null;
		}
	}
	
	public static void startDashboardGUI(LoginCredential user){
		if(dashboardGUI == null){
			dashboardGUI = new DashboardGUI(user);
			dashboardGUI.setVisible(true);
		}
	}
	
	public static void closeDashboardGUI(){
		if(dashboardGUI != null){
			dashboardGUI.setVisible(false);
			dashboardGUI.dispose();
			dashboardGUI = null;
		}
	}
	
	public static void setCurrentUser(LoginCredential user){
		currentUser = user;
	}
	
	public static String getUserName(){
		return currentUser.getUserName();
	}
	
	public static void initThesaurus(){
		String filename = "thesaurus.ser";
		Question q = new Question("Dit is de ROOT vraag", "Dit is de de beschrijving van de ROOT vraag!");
		q.addItem(new Answer("Antwoord 1", "Dit is de omschrijving van antwoord 1"));
		Question q2 = new Question("Antwoord 2, met vervolg vraag ", "Dit is de vervolg vraag" , "Dit is de omschrijving van de vervolg vraag.");
		q.addItem(q2);
		q2.addItem(new Answer("sub-antwoord 1", "Sub-antwoord 1 omschrijving!"));
		q2.addItem(new Answer("sub-antwoord 2", "Sub-antwoord 2 omschrijving!"));

	    // save the object to file
	    FileOutputStream fos = null;
	    ObjectOutputStream out = null;
	    try {
	      fos = new FileOutputStream(filename);
	      out = new ObjectOutputStream(fos);
	      out.writeObject(q);

	      out.close();
	    } catch (Exception ex) {
	      ex.printStackTrace();
	    }
	}
}
